﻿@{

    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>物料产品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.mobile.css" rel="stylesheet" />
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/scripts/jquery-3.4.1.min.js"></script>
</head>
<body>
    <div class="layui-inline">
        物料编码:
        <div class="layui-inline">
            <input type="text" class="layui-input layui-input-inline" id="ItemCode" value="" placeholder="请输入物料编码" />
        </div>
        物料名称:
        <div class="layui-inline">
            <input type="text" class="layui-input layui-input-inline" id="ItemName" value="" placeholder="请输入物料编码" />
        </div>
        <button type="button" lay-submit lay-filter="demo-table-search" class="layui-btn layui-btn layui-btn-normal">
            <i class="layui-icon layui-icon-search"></i>查询
        </button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
    <div style="padding: 16px;">
        <table class="layui-hide" id="test" lay-filter="test"></table>
    </div>
    <div style="position: fixed; bottom: 0; left: 50%; transform: translateX(-50%);">
        <button class="layui-btn" onclick="add()">确定</button>
        <button class="layui-btn layui-btn-danger" onclick="cancel()">取消</button>
    </div>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script>
        var table = layui.table;
        var dropdown = layui.dropdown;
        var layer = layui.layer;
        var util = layui.util;
        function cancel() {
            var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
            parent.layer.close(index);//再执行关闭
        }
        function add() {
            // 获取选中行数据并将名称传给父页面
            var checkStatus = table.checkStatus('test');
            var data = checkStatus.data;
            console.log(data[0]);
            if (data.length == 0) {
                layer.msg("请选择数据");
                return;
            }
            if (data.length > 1) {
                layer.msg("只能选择一行数据");
                return;
            }

            var ItemCode = data[0].ItemCode;
            var ItemName = data[0].ItemName;
            var UnitOfMeasure = data[0].UnitOfMeasure;
            var Specification = data[0].Specification;
            var Id = data[0].Id;
            // 通过父页面的接口将供应商名称传给父页面
            sessionStorage.setItem("ItemCodeFromPopup", ItemCode);
            sessionStorage.setItem("ItemNameFromPopup", ItemName);
            sessionStorage.setItem("UnitOfMeasureFromPopup", UnitOfMeasure);
            sessionStorage.setItem("SpecificationFromPopup", Specification);
            sessionStorage.setItem("FromPopupId", Id);
            // 关闭当前弹出层
            cancel();
        }
        layui.use(function () {
            // *******************搜索**********************
            var form = layui.form;
            form.on('submit(demo-table-search)', function (data) {
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                var code = $("#ItemCode").val();
                var name = $("#ItemName").val();
                table.reload('test', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: {
                        code: code,
                        name: name
                    }
                });
                layer.msg('搜索成功');
                return false; // 阻止默认 form 跳转
            });
            ///***************************显示************************
            // 创建渲染实例
            function show() {
                table.render({
                    elem: '#test',
                    url: '/MainData/GetMdItems',
                    page: true,
                    limit: 10,
                    limits: [10, 20, 50, 100],
                    cols: [[
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'ItemCode', fixed: 'left', title: '物料编码', sort: true, totalRow: '合计：' },
                        { field: 'ItemName', title: '物料名称' },
                        { field: 'Specification', title: '规格型号' },
                        { field: 'UnitOfMeasure', title: '单位' },
                        {
                            field: 'ItemOrProduct', title: '物料/产品', templet: function (row) {
                                var tmp = row.ItemOrProduct == 'item' ? "物料" : "产品";
                                return tmp;
                            }
                        },
                        { field: 'ItemTypeName', title: '所属分类' },
                        {
                            field: 'EnableFlag', title: '是否启用', templet: function (row) {
                                var tmp = row.EnableFlag == 'Y' ? "是" : "否";
                                return tmp;
                            }
                        },
                        {
                            field: 'SafeStockFlag', title: '设置安全库存', templet: function (row) {
                                var tmp = row.SafeStockFlag == 'Y' ? "是" : "否";
                                return tmp;
                            }
                        },
                        { field: 'CreateTime', title: '创建时间' },
                    ]],
                    error: function (res, msg) {
                        console.log(res, msg)
                    }
                });
            }
            show();
            // 触发表格复选框选择
            table.on('checkbox(test)', function (obj) {
                console.log(obj)
            });
        });
    </script>
</body>
</html>